<?php


namespace app\zmt\controller;


use app\zmt\model\GoodsattrModel;
use app\zmt\model\GoodsModel;
use app\zmt\model\ProductattrModel;
use cmf\controller\AdminBaseController;
use think\facade\Request;

class GoodsController extends AdminBaseController
{
    //显示
    public function index()
    {
        //搜索
        $keyword = input('keyword/s','');
        $where = [];
        if($keyword){
            $where[] = ['name','like','%'.$keyword.'%'];
        }

        //商品列表显示
        $list = GoodsModel::order(['list_order'=>'DESC','id'=>'DESC'])
            ->where($where)
            ->paginate(5);
        $this->assign('list',$list);

        return $this->fetch('goods');

    }

    //排序
    public function px()
    {
        $good = new GoodsModel();
        parent::listOrders($good);
        $this->success('排序成功');
    }

    //添加显示
    public function add()
    {

        return $this->fetch();
    }

    //添加
    public function addPost()
    {
        $data = input('post.');

//        halt($data);
        $data['image_more'] = implode(',',$data['image_more']);

        //图片获取器
        $data['image'] = cmf_get_image_url($data['image']);

        //验证器验证
//        $validata = new NavValidata();
//        $result = $validata->scene('add')->check($data);
//        if(!$result){
//            $this->error($validata->getError());
//        }

        $add = GoodsModel::create($data);

        if($add){
            $this->success('添加成功','goods/index');
        } else {
            $this->error('添加失败');
        }
    }

    //编辑显示
    public function edit()
    {
        //接收id
        $id = input('id','');
        //编辑页面显示与数据还原
        $list = GoodsModel::find($id);

        $this->assign('list',$list);
        return $this->fetch();

    }

    //编辑
    public function editPost()
    {
        //接收编辑数据
        $data = input('post.');
        //图片获取器
        $data['image'] = cmf_get_image_url($data['image']);


        //验证器验证
        //$validata = new NavValidata();
        //$result = $validata->scene('add')->check($data);
        //if(!$result){
        //$this->error($validata->getError());
        //}

        //编辑数据逻辑
        $edit = GoodsModel::find($data['id']);
        if(!$edit){
            $this->error('未查询到数据');
        }
        $edit->name = $data['name'];
        $edit->image = $data['image'];
        $edit->yf = $data['yf'];
        $edit->status = $data['status'];
        $edit->jy = $data['jy'];

        if($edit->save()){
            $this->success('保存成功','goods/index');
        }else{
            $this->error('保存失败');
        }
    }

    //删除逻辑
    public function delete()
    {
        //接收删除数据id
        $id = input('id','');

        if(GoodsModel::destroy($id)){
            $this->success('删除成功','goods/index');
        }else{
            $this->error('删除失败');
        }

    }

    public function show(\think\Request $request){
        $id = $request->param('id');
//        halt($id);
        $list = GoodsModel::find($id);

        if($list['jy'] == 1){
            $list['jy'] = 0;
        }else{
            $list['jy'] = 1;
        }

        $edit = GoodsModel::find($id);
        $edit->jy = $list['jy'];
        if($edit->save()){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }

    }

    public function setGoodsAttr()
    {
        $goodsId = input('goods_id');
        $this->assign('goods_id',$goodsId);
        return $this->fetch('add_goods_attr_value');
    }

    public function goodsAttr()
    {
        $attr = input('post.attr');
        $goods_id = input('post.goods_id');
        if(count($attr)<1){
            $this->error('至少配置一个');
        }
        GoodsModel::where('goods_id','=',$goods_id)
            ->delete();
        $insert_data = [];

        foreach ($attr as $attrs){
            $insert_data[] = [
                'goods_id'=>$goods_id,
                'attr'=>$attrs['name']
            ];
        }
        $data = GoodsattrModel::create($insert_data);
        if($data){
            $this->success('成功');
        }
    }

    public function getGoodsAttr()
    {
        $goods_id = input('post.goods_id');

        $data = GoodsattrModel::where('goods_id','=',$goods_id)
            ->select();

        return json(['code'=>1,'msg'=>'数据','data'=>$data]);
    }

    public function setAttr()
    {
        $goods_id = input('goods_id');
        $this->assign('goods_id',$goods_id);
        return $this->fetch();
    }


    public function AttrIndex()
    {
        $goods_id = input('post.goods_id');
        $data = ProductattrModel::where('goods_attr','=',$goods_id)
            ->select();

        return json(['code'=>1,'msg'=>'数据','data'=>$data]);
    }

    public function sentAttrIndex()
    {
        $attr= input('post.attr');
        $goods_id = input('post.goods_id');
        $data = [];
        foreach ($attr as $attrs){
            $data[] = [
                'goods_id'=>$goods_id,
                'attr_id'=>$attrs['attr'],
                'attr_value'=>$attrs['attr_value']
            ];
        }
        ProductattrModel::create($data);
    }

























}